//
//  SpecialAnnotationView.m
//  CarShare
//
//  Created by Pro on 2018/1/5.
//

#import "SpecialAnnotationView.h"

#define kWidth  35.f
#define kHeight 35.f

#define kHoriMargin 5.f
#define kVertMargin 5.f

#define kPortraitWidth  31.f
#define kPortraitHeight 31.f

#define kCalloutWidth   200.0
#define kCalloutHeight  70.0

@interface SpecialAnnotationView()

@property (nonatomic, strong) UIImageView *portraitImageView;
@property (nonatomic, strong) UIImageView *redBackImageView;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation SpecialAnnotationView

@synthesize calloutView;
@synthesize portraitImageView  = _portraitImageView;
@synthesize redBackImageView   = _redBackImageView;
@synthesize nameLabel          = _nameLabel;



- (void)btnAction
{
    CLLocationCoordinate2D coorinate = [self.annotation coordinate];
    
    NSLog(@"coordinate = {%f, %f}", coorinate.latitude, coorinate.longitude);
    NSLog(@"coordinate = {%f, %f}", coorinate.latitude, coorinate.longitude);
    NSLog(@"coordinate = {%f, %f}", coorinate.latitude, coorinate.longitude);
    NSLog(@"coordinate = {%f, %f}", coorinate.latitude, coorinate.longitude);
    
}

#pragma mark - Override

- (NSString *)name
{
    return self.nameLabel.text;
}

- (void)setName:(NSString *)name
{
    self.nameLabel.text = name;
}




#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
                
        self.bounds = CGRectMake(0.f, 0.f, kWidth, kHeight);
        
        self.backgroundColor = [UIColor clearColor];
        
        self.portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kHoriMargin / 2, kVertMargin / 2, kPortraitWidth, kPortraitHeight)];
        self.portraitImageView.image = [UIImage imageNamed:@"parking_lot_p"];
        [self addSubview:self.portraitImageView];
        
        
        self.redBackImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kHoriMargin / 2 + 7, - 18, 17, 22)];
        self.redBackImageView.image = [UIImage imageNamed:@"quantity"];
        [self addSubview:self.redBackImageView];

        /* Create name label. */
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kHoriMargin / 2 + 8,
                                                                   - 17,
                                                                   15,
                                                                   15)];
        
        UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        button.backgroundColor = [UIColor clearColor];
        [self addSubview:button];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        self.nameLabel.text = annotation.subtitle;
        self.nameLabel.backgroundColor  = [UIColor clearColor];
        self.nameLabel.textAlignment    = NSTextAlignmentCenter;
        self.nameLabel.textColor        = [UIColor whiteColor];
        self.nameLabel.font             = [UIFont systemFontOfSize:14.f];
        [self addSubview:self.nameLabel];
        
        self.needIndex = annotation.title;
    }
    
    return self;
}

- (void)buttonAction{
    
    if ([self.delegate respondsToSelector:@selector(buttonBeTouched:)]) {
        [self.delegate buttonBeTouched:self];
    }
}

@end
